package presentation.simplecomponents;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JLabel;

public class SimpleTipLabel extends JLabel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Color bg_color;
	private String text;
	private boolean isMove;
	private int time;

	public SimpleTipLabel(String text, Color bg_color, boolean isMove) {
		this.bg_color = bg_color;
		this.text = text;
		time = 0;
		this.isMove = isMove;
		if (isMove) {
			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					while (true) {
						try {
							Thread.sleep(50);
							time++;
							repaint();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			});
			thread.start();
		}
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
		repaint();
	}

	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setColor(bg_color);
		g2d.fillRect(0, 0, getWidth(), getHeight());

		g2d.setFont(new Font("Microsoft Yahei", 1, 20));
		g2d.setColor(Color.white);

		if (isMove) {
			int x = getWidth() - time * 2;
			int y = getHeight() / 2;
			if (x < -text.length() * 20) {
				time = 0;
			}
			g2d.drawString(text, x, y);
		}else{
			int h = getHeight()/2;
			int w = getWidth()/2 - (int)(8*(text.length()));
			g2d.drawString(text, w, h);
		}

	}

}
